{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Occupation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Introduction:\n",
    "\n",
    "Special thanks to: https://github.com/justmarkham for sharing the dataset and materials.\n",
    "\n",
    "### Step 1. Import the necessary libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T06:03:08.927369Z",
     "start_time": "2024-01-13T06:03:08.208829Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 2. Import the dataset from this [address](https://raw.githubusercontent.com/justmarkham/DAT8/master/data/u.user). "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 3. Assign it to a variable called users."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T06:04:22.518027Z",
     "start_time": "2024-01-13T06:04:22.255374Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "     user_id  age gender     occupation zip_code\n0          1   24      M     technician    85711\n1          2   53      F          other    94043\n2          3   23      M         writer    32067\n3          4   24      M     technician    43537\n4          5   33      F          other    15213\n..       ...  ...    ...            ...      ...\n938      939   26      F        student    33319\n939      940   32      M  administrator    02215\n940      941   20      M        student    97229\n941      942   48      F      librarian    78209\n942      943   22      M        student    77841\n\n[943 rows x 5 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>age</th>\n      <th>gender</th>\n      <th>occupation</th>\n      <th>zip_code</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>24</td>\n      <td>M</td>\n      <td>technician</td>\n      <td>85711</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>53</td>\n      <td>F</td>\n      <td>other</td>\n      <td>94043</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>23</td>\n      <td>M</td>\n      <td>writer</td>\n      <td>32067</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>4</td>\n      <td>24</td>\n      <td>M</td>\n      <td>technician</td>\n      <td>43537</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>5</td>\n      <td>33</td>\n      <td>F</td>\n      <td>other</td>\n      <td>15213</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>938</th>\n      <td>939</td>\n      <td>26</td>\n      <td>F</td>\n      <td>student</td>\n      <td>33319</td>\n    </tr>\n    <tr>\n      <th>939</th>\n      <td>940</td>\n      <td>32</td>\n      <td>M</td>\n      <td>administrator</td>\n      <td>02215</td>\n    </tr>\n    <tr>\n      <th>940</th>\n      <td>941</td>\n      <td>20</td>\n      <td>M</td>\n      <td>student</td>\n      <td>97229</td>\n    </tr>\n    <tr>\n      <th>941</th>\n      <td>942</td>\n      <td>48</td>\n      <td>F</td>\n      <td>librarian</td>\n      <td>78209</td>\n    </tr>\n    <tr>\n      <th>942</th>\n      <td>943</td>\n      <td>22</td>\n      <td>M</td>\n      <td>student</td>\n      <td>77841</td>\n    </tr>\n  </tbody>\n</table>\n<p>943 rows × 5 columns</p>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "users = pd.read_csv(\"https://raw.githubusercontent.com/justmarkham/DAT8/master/data/u.user\",sep= \"|\")\n",
    "users"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 4. Discover what is the mean age per occupation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T06:04:44.513954Z",
     "start_time": "2024-01-13T06:04:44.497513Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "occupation\nadministrator    38.746835\nartist           31.392857\ndoctor           43.571429\neducator         42.010526\nengineer         36.388060\nentertainment    29.222222\nexecutive        38.718750\nhealthcare       41.562500\nhomemaker        32.571429\nlawyer           36.750000\nlibrarian        40.000000\nmarketing        37.615385\nnone             26.555556\nother            34.523810\nprogrammer       33.121212\nretired          63.071429\nsalesman         35.666667\nscientist        35.548387\nstudent          22.081633\ntechnician       33.148148\nwriter           36.311111\nName: age, dtype: float64"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "users.groupby(\"occupation\")['age'].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 5. Discover the Male ratio per occupation and sort it from the most to the least"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T06:16:30.485178Z",
     "start_time": "2024-01-13T06:16:30.467652Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "       occupation  MaleRatio\n2          doctor   1.000000\n4        engineer   0.970149\n19     technician   0.962963\n15        retired   0.928571\n14     programmer   0.909091\n6       executive   0.906250\n17      scientist   0.903226\n5   entertainment   0.888889\n9          lawyer   0.833333\n16       salesman   0.750000\n3        educator   0.726316\n18        student   0.693878\n13          other   0.657143\n11      marketing   0.615385\n20         writer   0.577778\n12           none   0.555556\n0   administrator   0.544304\n1          artist   0.535714\n10      librarian   0.431373\n7      healthcare   0.312500\n8       homemaker   0.142857",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>occupation</th>\n      <th>MaleRatio</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2</th>\n      <td>doctor</td>\n      <td>1.000000</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>engineer</td>\n      <td>0.970149</td>\n    </tr>\n    <tr>\n      <th>19</th>\n      <td>technician</td>\n      <td>0.962963</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>retired</td>\n      <td>0.928571</td>\n    </tr>\n    <tr>\n      <th>14</th>\n      <td>programmer</td>\n      <td>0.909091</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>executive</td>\n      <td>0.906250</td>\n    </tr>\n    <tr>\n      <th>17</th>\n      <td>scientist</td>\n      <td>0.903226</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>entertainment</td>\n      <td>0.888889</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>lawyer</td>\n      <td>0.833333</td>\n    </tr>\n    <tr>\n      <th>16</th>\n      <td>salesman</td>\n      <td>0.750000</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>educator</td>\n      <td>0.726316</td>\n    </tr>\n    <tr>\n      <th>18</th>\n      <td>student</td>\n      <td>0.693878</td>\n    </tr>\n    <tr>\n      <th>13</th>\n      <td>other</td>\n      <td>0.657143</td>\n    </tr>\n    <tr>\n      <th>11</th>\n      <td>marketing</td>\n      <td>0.615385</td>\n    </tr>\n    <tr>\n      <th>20</th>\n      <td>writer</td>\n      <td>0.577778</td>\n    </tr>\n    <tr>\n      <th>12</th>\n      <td>none</td>\n      <td>0.555556</td>\n    </tr>\n    <tr>\n      <th>0</th>\n      <td>administrator</td>\n      <td>0.544304</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>artist</td>\n      <td>0.535714</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>librarian</td>\n      <td>0.431373</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>healthcare</td>\n      <td>0.312500</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>homemaker</td>\n      <td>0.142857</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "occupation_ratio = users.groupby(\"occupation\")['gender'].agg(lambda x: (x == 'M').sum() / len(x)).reset_index()\n",
    "# Rename the columns for clarity\n",
    "occupation_ratio.rename(columns={\"gender\": \"MaleRatio\"},inplace=True)\n",
    "occupation_ratio.sort_values(by = \"MaleRatio\",ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 6. For each occupation, calculate the minimum and maximum ages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T06:17:48.802968Z",
     "start_time": "2024-01-13T06:17:48.795473Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "               max  min\noccupation             \nadministrator   70   21\nartist          48   19\ndoctor          64   28\neducator        63   23\nengineer        70   22\nentertainment   50   15\nexecutive       69   22\nhealthcare      62   22\nhomemaker       50   20\nlawyer          53   21\nlibrarian       69   23\nmarketing       55   24\nnone            55   11\nother           64   13\nprogrammer      63   20\nretired         73   51\nsalesman        66   18\nscientist       55   23\nstudent         42    7\ntechnician      55   21\nwriter          60   18",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>max</th>\n      <th>min</th>\n    </tr>\n    <tr>\n      <th>occupation</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>administrator</th>\n      <td>70</td>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>artist</th>\n      <td>48</td>\n      <td>19</td>\n    </tr>\n    <tr>\n      <th>doctor</th>\n      <td>64</td>\n      <td>28</td>\n    </tr>\n    <tr>\n      <th>educator</th>\n      <td>63</td>\n      <td>23</td>\n    </tr>\n    <tr>\n      <th>engineer</th>\n      <td>70</td>\n      <td>22</td>\n    </tr>\n    <tr>\n      <th>entertainment</th>\n      <td>50</td>\n      <td>15</td>\n    </tr>\n    <tr>\n      <th>executive</th>\n      <td>69</td>\n      <td>22</td>\n    </tr>\n    <tr>\n      <th>healthcare</th>\n      <td>62</td>\n      <td>22</td>\n    </tr>\n    <tr>\n      <th>homemaker</th>\n      <td>50</td>\n      <td>20</td>\n    </tr>\n    <tr>\n      <th>lawyer</th>\n      <td>53</td>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>librarian</th>\n      <td>69</td>\n      <td>23</td>\n    </tr>\n    <tr>\n      <th>marketing</th>\n      <td>55</td>\n      <td>24</td>\n    </tr>\n    <tr>\n      <th>none</th>\n      <td>55</td>\n      <td>11</td>\n    </tr>\n    <tr>\n      <th>other</th>\n      <td>64</td>\n      <td>13</td>\n    </tr>\n    <tr>\n      <th>programmer</th>\n      <td>63</td>\n      <td>20</td>\n    </tr>\n    <tr>\n      <th>retired</th>\n      <td>73</td>\n      <td>51</td>\n    </tr>\n    <tr>\n      <th>salesman</th>\n      <td>66</td>\n      <td>18</td>\n    </tr>\n    <tr>\n      <th>scientist</th>\n      <td>55</td>\n      <td>23</td>\n    </tr>\n    <tr>\n      <th>student</th>\n      <td>42</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>technician</th>\n      <td>55</td>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>writer</th>\n      <td>60</td>\n      <td>18</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "users.groupby(\"occupation\")['age'].agg({\"min\",\"max\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 7. For each combination of occupation and gender, calculate the mean age"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T06:18:45.309556Z",
     "start_time": "2024-01-13T06:18:45.296298Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "occupation     gender\nadministrator  F         40.638889\n               M         37.162791\nartist         F         30.307692\n               M         32.333333\ndoctor         M         43.571429\neducator       F         39.115385\n               M         43.101449\nengineer       F         29.500000\n               M         36.600000\nentertainment  F         31.000000\n               M         29.000000\nexecutive      F         44.000000\n               M         38.172414\nhealthcare     F         39.818182\n               M         45.400000\nhomemaker      F         34.166667\n               M         23.000000\nlawyer         F         39.500000\n               M         36.200000\nlibrarian      F         40.000000\n               M         40.000000\nmarketing      F         37.200000\n               M         37.875000\nnone           F         36.500000\n               M         18.600000\nother          F         35.472222\n               M         34.028986\nprogrammer     F         32.166667\n               M         33.216667\nretired        F         70.000000\n               M         62.538462\nsalesman       F         27.000000\n               M         38.555556\nscientist      F         28.333333\n               M         36.321429\nstudent        F         20.750000\n               M         22.669118\ntechnician     F         38.000000\n               M         32.961538\nwriter         F         37.631579\n               M         35.346154\nName: age, dtype: float64"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "users.groupby([\"occupation\",\"gender\"])['age'].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 8.  For each occupation present the percentage of women and men"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T07:04:30.541919Z",
     "start_time": "2024-01-13T07:04:30.525009Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "gender                 F          M\noccupation                         \nadministrator  13.186813   6.417910\nartist          4.761905   2.238806\ndoctor          0.000000   1.044776\neducator        9.523810  10.298507\nengineer        0.732601   9.701493\nentertainment   0.732601   2.388060\nexecutive       1.098901   4.328358\nhealthcare      4.029304   0.746269\nhomemaker       2.197802   0.149254\nlawyer          0.732601   1.492537\nlibrarian      10.622711   3.283582\nmarketing       3.663004   2.388060\nnone            1.465201   0.746269\nother          13.186813  10.298507\nprogrammer      2.197802   8.955224\nretired         0.366300   1.940299\nsalesman        1.098901   1.343284\nscientist       1.098901   4.179104\nstudent        21.978022  20.298507\ntechnician      0.366300   3.880597\nwriter          6.959707   3.880597",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>gender</th>\n      <th>F</th>\n      <th>M</th>\n    </tr>\n    <tr>\n      <th>occupation</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>administrator</th>\n      <td>13.186813</td>\n      <td>6.417910</td>\n    </tr>\n    <tr>\n      <th>artist</th>\n      <td>4.761905</td>\n      <td>2.238806</td>\n    </tr>\n    <tr>\n      <th>doctor</th>\n      <td>0.000000</td>\n      <td>1.044776</td>\n    </tr>\n    <tr>\n      <th>educator</th>\n      <td>9.523810</td>\n      <td>10.298507</td>\n    </tr>\n    <tr>\n      <th>engineer</th>\n      <td>0.732601</td>\n      <td>9.701493</td>\n    </tr>\n    <tr>\n      <th>entertainment</th>\n      <td>0.732601</td>\n      <td>2.388060</td>\n    </tr>\n    <tr>\n      <th>executive</th>\n      <td>1.098901</td>\n      <td>4.328358</td>\n    </tr>\n    <tr>\n      <th>healthcare</th>\n      <td>4.029304</td>\n      <td>0.746269</td>\n    </tr>\n    <tr>\n      <th>homemaker</th>\n      <td>2.197802</td>\n      <td>0.149254</td>\n    </tr>\n    <tr>\n      <th>lawyer</th>\n      <td>0.732601</td>\n      <td>1.492537</td>\n    </tr>\n    <tr>\n      <th>librarian</th>\n      <td>10.622711</td>\n      <td>3.283582</td>\n    </tr>\n    <tr>\n      <th>marketing</th>\n      <td>3.663004</td>\n      <td>2.388060</td>\n    </tr>\n    <tr>\n      <th>none</th>\n      <td>1.465201</td>\n      <td>0.746269</td>\n    </tr>\n    <tr>\n      <th>other</th>\n      <td>13.186813</td>\n      <td>10.298507</td>\n    </tr>\n    <tr>\n      <th>programmer</th>\n      <td>2.197802</td>\n      <td>8.955224</td>\n    </tr>\n    <tr>\n      <th>retired</th>\n      <td>0.366300</td>\n      <td>1.940299</td>\n    </tr>\n    <tr>\n      <th>salesman</th>\n      <td>1.098901</td>\n      <td>1.343284</td>\n    </tr>\n    <tr>\n      <th>scientist</th>\n      <td>1.098901</td>\n      <td>4.179104</td>\n    </tr>\n    <tr>\n      <th>student</th>\n      <td>21.978022</td>\n      <td>20.298507</td>\n    </tr>\n    <tr>\n      <th>technician</th>\n      <td>0.366300</td>\n      <td>3.880597</td>\n    </tr>\n    <tr>\n      <th>writer</th>\n      <td>6.959707</td>\n      <td>3.880597</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gender_percentage = users.groupby([\"occupation\",\"gender\"]).size().unstack(fill_value=0)\n",
    "gender_percentage.div(gender_percentage.sum(axis=1), axis=0) * 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "data": {
      "text/plain": "occupation     gender\nadministrator  F          45.569620\n               M          54.430380\nartist         F          46.428571\n               M          53.571429\ndoctor         M         100.000000\neducator       F          27.368421\n               M          72.631579\nengineer       F           2.985075\n               M          97.014925\nentertainment  F          11.111111\n               M          88.888889\nexecutive      F           9.375000\n               M          90.625000\nhealthcare     F          68.750000\n               M          31.250000\nhomemaker      F          85.714286\n               M          14.285714\nlawyer         F          16.666667\n               M          83.333333\nlibrarian      F          56.862745\n               M          43.137255\nmarketing      F          38.461538\n               M          61.538462\nnone           F          44.444444\n               M          55.555556\nother          F          34.285714\n               M          65.714286\nprogrammer     F           9.090909\n               M          90.909091\nretired        F           7.142857\n               M          92.857143\nsalesman       F          25.000000\n               M          75.000000\nscientist      F           9.677419\n               M          90.322581\nstudent        F          30.612245\n               M          69.387755\ntechnician     F           3.703704\n               M          96.296296\nwriter         F          42.222222\n               M          57.777778\nName: gender, dtype: float64"
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# create a data frame and apply count to gender\n",
    "gender_ocup = users.groupby(['occupation', 'gender']).agg({'gender': 'count'})\n",
    "# create a DataFrame and apply count for each occupation\n",
    "occup_count = users.groupby(['occupation']).agg('count')\n",
    "\n",
    "# divide the gender_ocup per the occup_count and multiply per 100\n",
    "occup_gender = gender_ocup.div(occup_count, level = \"occupation\") * 100\n",
    "# present all rows from the 'gender column'\n",
    "occup_gender.loc[: , 'gender']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T07:05:51.916475Z",
     "start_time": "2024-01-13T07:05:51.900599Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3 (ipykernel)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
